local skill = fk.CreateSkill{
  name = "iberia__lure_tiger_skill",
}

Fk:loadTranslationTable{
  [skill.name] = "调虎离山",
  ["@@iberia__lure_tiger-turn"] = "调离",
}

local I = require "packages/iberia/utility"
local H = require "packages/hegemony/util"

skill:addEffect("cardskill", {
  prompt = "#iberia__lure_tiger_skill",
  can_use = Util.CanUse,
  min_target_num = 1,
  max_target_num = 2,
  mod_target_filter = function(self, player, to_select, selected)
    return to_select ~= player
  end,
  on_use = function(self, room, use)
    if I.isCounsellor(use.from) and #use:getAllTargets() == 1 then
      use.from:drawCards(1, "iberia__lure_tiger")
    end
  end,
  target_filter = Util.CardTargetFilter,
  on_effect = function(self, room, effect)
    local target = effect.to
    room:setPlayerMark(target, "@@iberia__lure_tiger-turn", 1)
    room:setPlayerMark(target, MarkEnum.PlayerRemoved .. "-turn", 1)
    room.logic:trigger(H.PlayerRemoved, target, {who = target})
  end,
})

---@param object Card|Player
---@param markName string
---@param suffixes string[]
---@return boolean
local function hasMark(object, markName, suffixes)
  if not object then return false end
  for mark, _ in pairs(object.mark) do
    if mark == markName then return true end
    if mark:startsWith(markName .. "-") then
      for _, suffix in ipairs(suffixes) do
        if mark:find(suffix, 1, true) then return true end
      end
    end
  end
  return false
end

skill:addEffect('prohibit', {
  name = "#iberia__lure_tiger_prohibit",
  global = true,
  prohibit_use = function(self, player, card)
    return hasMark(player, "@@iberia__lure_tiger", MarkEnum.TempMarkSuffix)
  end,
  is_prohibited = function(self, from, to, card)
    return hasMark(to, "@@iberia__lure_tiger", MarkEnum.TempMarkSuffix)
  end,
})

skill:addEffect(fk.PreHpRecover, {
  global = true,
  can_refresh = function(self, event, target, player, data)
    return target == player and hasMark(player, "@@iberia__lure_tiger", MarkEnum.TempMarkSuffix)
  end,
  on_refresh = function(self, event, target, player, data)
    data:preventRecover()
  end,
})

skill:addEffect(fk.PreHpLost, {
  global = true,
  can_refresh = function(self, event, target, player, data)
    return target == player and hasMark(player, "@@iberia__lure_tiger", MarkEnum.TempMarkSuffix)
  end,
  on_refresh = function(self, event, target, player, data)
    data:preventHpLost()
  end,
})

skill:addEffect(fk.DamageInflicted, {
  global = true,
  can_refresh = function(self, event, target, player, data)
    return target == player and hasMark(player, "@@iberia__lure_tiger", MarkEnum.TempMarkSuffix)
  end,
  on_refresh = function(self, event, target, player, data)
    data:preventDamage()
  end,
})

return skill
